/**
 * Copyright (c) 2005 - 2007 FeyaSoft Corp. All Rights Reserved.
 */
package com.feyaSoft.home.services.feedback;

import java.util.Set;

import org.apache.log4j.Logger;

import com.feyaSoft.home.dao.base.BaseSearchResult;
import com.feyaSoft.home.dao.feedback.FeedbackDao;
import com.feyaSoft.home.dao.feedback.FeedbackSearchCriteria;
import com.feyaSoft.home.hibernate.feedback.Feedback;
import com.feyaSoft.home.services.base.SearchBaseInfo;
import com.feyaSoft.plugin.util.CSVParser;
import com.feyaSoft.plugin.util.ObjectCheck;

/**
 * @author Fenqiang Zhuang
 * @Jun 10, 2007
 * 
 * This file is used to .....
 */
public class FeedbackServiceImpl implements FeedbackService {
	// log file
	private final static Logger log = Logger
			.getLogger(FeedbackServiceImpl.class);
	private FeedbackDao feedbackDAO;

	/**
	 * get a list of class now...
	 */
	public BaseSearchResult getList(SearchBaseInfo searchBaseInfo)
			throws Exception {
		// set search criteria now
		FeedbackSearchCriteria searchCriteria = new FeedbackSearchCriteria();

		// set find field/value
		String field = searchBaseInfo.getField().trim();
		String findValue = searchBaseInfo.getFieldValue().trim();

		// see the field value
		if (field.equalsIgnoreCase("subject")
				&& (!ObjectCheck.isEmptyString(findValue))) {
			CSVParser stringParser = new CSVParser(findValue, true);
			Set stringInSet = stringParser.getAllFieldsInSet();
			searchCriteria.setSubjects(stringInSet);
		}

		// deal with status now - status and author/receiver
		String status = ((ListFeedbackInfo) searchBaseInfo).getStatus().trim();
		searchCriteria.setStatus(status);
		
		// check to see whether it is parentId selected
		Long parentId = ((ListFeedbackInfo)searchBaseInfo).getParentId();
	    searchCriteria.setParentId(parentId);

		// set default search order
		searchCriteria.setOrderProperty("updateDate");

		int rowsPerPage = searchBaseInfo.getRowsPerPage();
		int firstResultPos = (searchBaseInfo.getCurrentPage() - 1)
				* rowsPerPage;
		searchCriteria.setFirstResult(new Long(firstResultPos));
		searchCriteria.setTotalResults(new Integer(rowsPerPage));
		searchCriteria.setNeedCount(Boolean.TRUE);

		return feedbackDAO.search(searchCriteria);
	}

	/**
	 * Create new feedback now ...
	 * 
	 * @param feedback
	 * @return
	 * @throws Exception
	 */
	public boolean createFeedback(Feedback feedback) throws Exception {
		boolean result = false;

		try {
			feedbackDAO.saveOrUpdate(feedback);
			result = true;

		} catch (Exception e) {
			// log
			log.error("create new feedback failed..." + e.getMessage());
		}

		return result;
	}

	/**
	 * load feedback now...
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public Feedback getFeedback(Long id) throws Exception {
		Feedback feedback = null;
		try {
			feedback = feedbackDAO.loadFeedbackById(id);
		} catch (Exception e) {
			log.error("load feedback by id failed: id = " + id);
			e.printStackTrace();
		}

		return feedback;
	}

	/**
	 * archive feedback now...
	 */
	public boolean archiveFeedback(Long fId) throws Exception {
		boolean result = false;

		try {
			Feedback feedback = feedbackDAO.loadFeedbackById(fId);
			feedback.setStatus("archive");
			feedbackDAO.saveOrUpdate(feedback);
			result = true;
		} catch (Exception e) {
			log.error("load feedback by id failed: id = " + fId);
			e.printStackTrace();
		}

		return result;
	}

	public void setFeedbackDAO(FeedbackDao feedbackDAO) {
		this.feedbackDAO = feedbackDAO;
	}

}
